package com.oa.util;

import javax.servlet.http.HttpSession;

import com.oa.system.entity.UserInfo;
import com.oa.user.dto.UserInfoDto;

/*
 * Session工具类，保存数据到Session和取出在Session中的数据
 * */
public class SessionUtil {
	public static final String USERNAME = "userName";
	public static final String USER_ID = "userId";
	public static final String NAME = "fullname";
	public static final String USERINFO = "userinfo";

	private SessionUtil() {
	}

	public static void removeAttribute(HttpSession session) {
		session.removeAttribute(USERNAME);
		session.removeAttribute(USER_ID);
		session.removeAttribute(NAME);
		session.removeAttribute(USERINFO);
	}

	public static String getUserName(HttpSession session) {
		return (String) session.getAttribute(USERNAME);
	}

	public static void setUserName(HttpSession session, String username) {
		session.setAttribute(USERNAME, username);
	}

	public static Long getUserId(HttpSession session) {
		return (Long) session.getAttribute(USER_ID);
	}

	public static void setUserId(HttpSession session, Long userId) {
		session.setAttribute(USER_ID, userId);
	}

	public static String getName(HttpSession session) {
		return (String) session.getAttribute(NAME);
	}

	public static void setName(HttpSession session, String name) {
		session.setAttribute(NAME, name);
	}

	public static UserInfoDto getUserInfo(HttpSession session) {
		String info = (String) session.getAttribute(USERINFO);
		return JsonUtil.jsonToObject(info, UserInfoDto.class);
	}

	public static void setUserInfo(HttpSession session, UserInfo userInfo) {
		UserInfoDto userInfoDto = UserInfoDto.toUserInfoDto(userInfo);
		String info = JsonUtil.objectToJson(userInfoDto);
		session.setAttribute(USERINFO, info);
	}

}
